package com.example.park.park

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.example.lib_base.entity.ParkBeanItem
import com.example.park.R
import com.example.park.databinding.ItemParkBinding

class ParkAdapter :PagingDataAdapter<ParkBeanItem, ParkAdapter.ParkViewHolder>(holl) {
    private var mOnItemClickListener: OnItemClickListener? = null
    private var mOnItemLongClickListener: OnItemLongClickListener? = null
    companion object{
        val holl =object :ItemCallback<ParkBeanItem>(){
            override fun areItemsTheSame(oldItem: ParkBeanItem, newItem: ParkBeanItem): Boolean {
                return oldItem==newItem
            }

            override fun areContentsTheSame(oldItem: ParkBeanItem, newItem: ParkBeanItem): Boolean {
                return oldItem.id==newItem.id
            }
        }
    }

    class ParkViewHolder(view:View):ViewHolder(view){
        val binding =DataBindingUtil.bind<ItemParkBinding>(view)
    }

    override fun onBindViewHolder(holder: ParkViewHolder, position: Int) {
        holder.binding?.bean=getItem(position)
        val item = getItem(position)
        holder.binding?.parkStatus?.apply {
            when(item?.spaceStatus){
                "空闲"->{
                    setBackgroundResource(R.drawable.shape_status)
                    setTextColor(holder.itemView.context.resources.getColor(R.color.green))
                }
                "已占用"->{
                    setBackgroundResource(R.drawable.shape_status)
                    setTextColor(holder.itemView.context.resources.getColor(R.color.blue))
                }
                "维修中"->{
                    setBackgroundResource(R.drawable.shape_status)
                    setTextColor(holder.itemView.context.resources.getColor(R.color.org))
                }
            }
        }
        holder.itemView.apply {
            setOnClickListener {
                mOnItemClickListener?.onItemClick(item!!)
            }
            setOnLongClickListener {
                mOnItemLongClickListener?.onItemLongClick()
                return@setOnLongClickListener true
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParkViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_park, parent, false)
        return ParkViewHolder(view)
    }

     fun setOnItemClickListener(listener: OnItemClickListener?) {
        this.mOnItemClickListener = listener
    }

     fun setOnItemLongClickListener(listener: OnItemLongClickListener?) {
        this.mOnItemLongClickListener = listener
    }
}
interface OnItemClickListener {
    fun onItemClick(item: ParkBeanItem)
}
interface OnItemLongClickListener {
    fun onItemLongClick():Boolean
}